﻿#include "Sprite.h"
#include<qdebug.h>
#include"Resource.h"
#include "Game.h"
#include"ProtectionCover.h"
#include"WeaponBank.h"
Sprite::Sprite()
{
    destroy();
   /* creatorWeaponBank();*/
}

Sprite::Sprite(int x, int y, QObject* parent):m_initPos(x,y)
{
    move(x, y);
    init();
    m_parent = parent;
}

Sprite::Sprite(int x, int y, QSize WindowSize, QObject* parent)
    :m_initPos(x, y)
{
    move(x, y);
    init();
    m_parent = parent;
    //qDebug() << getWindowSize();
    setWindowSize(WindowSize);
    //qDebug() << getCollider();
    creatorWeaponBank();
    //设置初始碰撞器
    setCollider(getCenterPos(), getSize().width() / 2.0, getSize().height() / 2.0);
}

Sprite::~Sprite()
{
    closeProtect();
    
}


void Sprite::init()
{
    addFrame("玩家正常","正常");
    getModel()["正常"].setDelayTime(300);
    addFrame("玩家死亡", "死亡");
    /*QSize size = getModel()["正常"].getFrame().size();
    (size.height() > size.width()) ? (m_r = size.width()) : (m_r= size.height());*/
    setHp(3);
    setMaxHp(3);
    Plane::init();
    setType(Type::player);
}
void Sprite::update()
{
    Plane::update();
    if (m_ProtectionCover != nullptr)
    {
        m_ProtectionCover->update();
        if (m_ProtectionCover->ProtectOver())
        {
            delete m_ProtectionCover;
            m_ProtectionCover = nullptr;
        }
    }
    if (m_WeaponBank != nullptr)
        m_WeaponBank->update();
    
}
void Sprite::draw(QPainter* painter)
{
    Plane::draw(painter);
    if (m_ProtectionCover != nullptr)
    {
        m_ProtectionCover->draw(painter);
    }
   
    painter->setPen(Qt::GlobalColor::black);
    if (m_WeaponBank != nullptr)
    {
        //qDebug() << "显示";
        m_WeaponBank->draw(painter);
    }
}
void Sprite::moveBy(int dx, int dy)
{
    if (m_pause)
        return;
    if (getWindowSize().isEmpty())
    {
        OutInfo;
        qDebug() << "请在外部调用setWindowSize,不然飞机将无法移动";
        return;
    }
    if (getHp() == 0 || !isActive())
        return;
    QPoint Pos= getPos() + QPoint(dx, dy);
    if (Pos.x() + getSize().width() / 2 > getWindowSize().width())
    {
        setX(getWindowSize().width()- getSize().width() / 2);
        return;
    }
    if (Pos.x()+ getSize().width() / 2 <0 )
    {
        setX(-getSize().width() / 2);
        return;
    }
    if (Pos.y() + getSize().height() > getWindowSize().height())
    {
        setY(getWindowSize().height()- getSize().height());
        return;
    }
    if (Pos.y() < 0)
    {
        setY(0);
        return;
    }
    move(getPos()+QPoint(dx, dy));
    //m_modelRect.moveTo(m_modelRect.topLeft()+ QPoint(dx, dy));
    /*qDebug() << "m_modelRect:" << m_modelRect;*/
    setCollider(getCenterPos(), getSize().width() / 2.0, getSize().height() / 2.0);
}

void Sprite::reduceHp(const int hp)
{
    Articles::reduceHp(hp);
    //血量还有的话开启保护罩
    if (getHp() > 0)
    {
        startProtect();
    }
}

void Sprite::setInitPos()
{
    move(m_initPos);
}

ProtectionCover* Sprite::getProtection()
{
    return m_ProtectionCover;
}

void Sprite::setProtectTime(size_t time)
{
    m_ProtectTime = time;
}

const size_t& Sprite::getProtectTime()
{
    return m_ProtectTime;
}

const int Sprite::getProtectRemainingTime()
{
    if (m_ProtectionCover != nullptr)
        return m_ProtectionCover->getProtectRemainingTime();
    return -1;
}

const bool Sprite::getProtectState()
{
    if(m_ProtectionCover!=nullptr)
        return m_ProtectionCover->getProtectState();
    return false;
}

void Sprite::startProtect()
{
    if (m_ProtectTime <= 0)
        return;
    if (m_ProtectionCover != nullptr)
        delete m_ProtectionCover;
    m_ProtectionCover = new ProtectionCover(*this, m_ProtectTime);
}

void Sprite::closeProtect()
{
    if (m_ProtectionCover != nullptr)
    {
        delete m_ProtectionCover;
        m_ProtectionCover == nullptr;
    }
}

const int Sprite::getTotalScore()
{
    return m_TotalScore;
}

void Sprite::addTotalScore(const int score)
{
    m_TotalScore+=score;
}

void Sprite::setMaxBombNum(const int num)
{
    m_maxBombNum = num;
}

const int Sprite::getBombNum()
{
    return m_BombNum;
}

void Sprite::addBombNum(const int num)
{
    ++m_BombNum;
    if (m_BombNum > m_maxBombNum)
        m_BombNum = m_maxBombNum;
}

void Sprite::reduceBombNum(const int num)
{
    --m_BombNum;
}


void Sprite::setPause(bool flag)
{
    Plane::setPause(flag);
    if (m_WeaponBank != nullptr)
        m_WeaponBank->setPause(flag);
}
